package com.lyd.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lyd.dao.FolderDao;
import com.lyd.entity.FileEntity;
import com.lyd.entity.FolderEntity;

public class FolderDaoImpl implements FolderDao {

	@Override
	public void addFolder(FolderEntity nodeName) {
		Connection conn = DOBC.getConn();
		String sql = "insert into table_file(node_code,node_name,parent_node_code,node_type)values(?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, nodeName.getNodeCode());
			ps.setString(2, nodeName.getNodeName());
			ps.setString(3, nodeName.getParentNodeCode());
			ps.setString(4, nodeName.getNodeType());
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);

	}

	@Override
	public boolean updateFolder(FolderEntity nodeName) {
		Connection conn = DOBC.getConn();
		String sql = "update table_file set node_code=? where node_name=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, nodeName.getNodeCode());
			ps.setString(2, nodeName.getNodeName());

			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}

	@Override
	public boolean deleteFolder(String nodeName) {
		Connection conn = DOBC.getConn();
		String sql = "update table_file set delete_flag=? where node_name=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "Y");
			ps.setString(2, nodeName);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<FolderEntity> listAll() {
		String sql = "select id,node_code,node_type,node_name,parent_node_code,delete_flag from table_file";
		Connection conn = DOBC.getConn();
		List<FolderEntity> list = new ArrayList();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			// rs结果集
			while (rs.next()) {
				FolderEntity folderEntity = new FolderEntity();
				folderEntity.setNodeCode(rs.getString(2));
				folderEntity.setNodeType(rs.getString(3));
				folderEntity.setNodeName(rs.getString(4));
				folderEntity.setParentNodeCode(rs.getString(5));
				// 循环添加到List
				list.add(folderEntity);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(rs, ps, conn);
		return list;
	}

	@Override
	public List<FolderEntity> listAll(String roleCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FolderEntity get(String node_name) {
		String sql = "select node_name,node_code,node_type,parent_node_code from table_file where node_name='"
				+ node_name + "'";
		FolderEntity folderEntity = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection conn = DOBC.getConn();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				folderEntity = new FolderEntity();
				folderEntity.setNodeName(rs.getString(1));
				folderEntity.setNodeCode(rs.getString(2));
				folderEntity.setNodeType(rs.getString(3));
				folderEntity.setParentNodeCode(rs.getString(4));
				return folderEntity;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DOBC.close(rs, ps, conn);
		}
		return folderEntity;
	}

}
